package com.cy.store.controller;

import com.cy.store.service.ICartService;
import com.cy.store.util.JsonResult;
import com.cy.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/carts")
public class CartController extends BaseController {
    @Autowired
    private ICartService cartService;

    /**
     * 插入订单
     *
     * @param pid
     * @param amount
     * @param session
     * @return
     */
    @RequestMapping("/add_to_cart")
    public JsonResult<Void> addToCart(Integer pid, Integer amount, HttpSession session) {
        cartService.addToCart(getUidFromSession(session), pid, amount, getUsernameFromSession(session));
        return new JsonResult<>(OK);
    }

    @RequestMapping({"/",""})
    public JsonResult<List<CartVO>> getVOByUid(HttpSession session) {
        List<CartVO> voByUid = cartService.getVOByUid(getUidFromSession(session));
        return new JsonResult<>(OK,voByUid);
    }

    /**
     * 增加购物车中货物数量
     * @param session
     * @return
     */
    @RequestMapping("{cid}/num/add")
    public JsonResult<Integer> addNum(Integer cid,HttpSession session) {
        Integer integer = cartService.addNum(cid, getUidFromSession(session), getUsernameFromSession(session));
        return new JsonResult<>(OK,integer);
    }

    /**
     * 购物车勾选货物
     * @param session
     * @return
     */
    @RequestMapping("/list")
    public JsonResult<List<CartVO>> getVOByUid(Integer[] cids,HttpSession session) {
        List<CartVO> voByCid = cartService.getVOByCid(getUidFromSession(session), cids);
        return new JsonResult<>(OK,voByCid);
    }
}
